package cn.ljy.authority.service.impl;

import cn.ljy.authority.dao.mapper.SysDailyMapper;
import cn.ljy.authority.dao.mapper.SysDailyTypeMapper;
import cn.ljy.authority.model.SysDailyType;
import cn.ljy.authority.model.SysDictItem;
import cn.ljy.authority.model.query.SysDailyQuery;
import cn.ljy.authority.model.query.SysDailyTypeQuery;
import cn.ljy.authority.service.SysDailyTypeService;
import cn.ljy.common.exception.model.MyWebException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class ISysDailyTypeService  extends IBaseService<SysDailyType, SysDailyTypeQuery> implements SysDailyTypeService {

    @Autowired
    private SysDailyTypeMapper mapper;
    @Autowired
    private SysDailyMapper sysDailyMapper;

    @Override
    protected void afterFindAll(List<SysDailyType> list) {
        for(SysDailyType dailyType:list){
            //添加文章数量
            SysDailyQuery sysDailyQuery=new SysDailyQuery();
            sysDailyQuery.setDailyType(dailyType);
            dailyType.setDailyNumber(sysDailyMapper.count(sysDailyQuery));
        }
    }

    @Override
    protected void beforeSave(SysDailyType sysDailyType) {
        //判断名字是否重名
        if(mapper.countByName(sysDailyType.getName())>0){
            throw new MyWebException("编辑失败：名称已存在");
        }
    }

    @Override
    protected void beforeUpdate(SysDailyType sysDailyType) {
        //判断名字是否重名
        if(mapper.countByNameAndIdNot(sysDailyType.getName(),sysDailyType.getId())>0){
            throw new MyWebException("编辑失败：名称已存在");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        List<SysDictItem> items=new ArrayList<>();
        for(Serializable id:ids) {
            // 获取该类别下的所有日志，如果有则不允许删除。
//            SysDictItemQuery sysDictItemQuery=new SysDictItemQuery();
//            sysDictItemQuery.setSysDict(new SysDict(Long.valueOf(id.toString())));
//            items.addAll(sysDictItemMapper.findAll(sysDictItemQuery));
        }
    }


}
